home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / gfx / show / vmpeg.lha / src / gethdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-06  |  29.7 KB  |  1,117 lines

  1. /* gethdr.c, header decoding                                                */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. #include "global.h"
  34.  
  35. /* private prototypes */
  36. static void sequence_header _ANSI_ARGS_((void));
  37. static void group_of_pictures_header _ANSI_ARGS_((void));
  38. static void picture_header _ANSI_ARGS_((void));
  39. static void extension_and_user_data _ANSI_ARGS_((void));
  40. static void sequence_extension _ANSI_ARGS_((void));
  41. static void sequence_display_extension _ANSI_ARGS_((void));
  42. static void quant_matrix_extension _ANSI_ARGS_((void));
  43. static void sequence_scalable_extension _ANSI_ARGS_((void));
  44. static void picture_display_extension _ANSI_ARGS_((void));
  45. static void picture_coding_extension _ANSI_ARGS_((void));
  46. static void picture_spatial_scalable_extension _ANSI_ARGS_((void));
  47. static void picture_temporal_scalable_extension _ANSI_ARGS_((void));
  48. static int  extra_bit_information _ANSI_ARGS_((void));
  49. static void copyright_extension _ANSI_ARGS_((void));
  50. static void user_data _ANSI_ARGS_((void));
  51. static void user_data _ANSI_ARGS_((void));
  52.  
  53.  
  54.  
  55.  
  56. /* introduced in September 1995 to assist spatial scalable decoding */
  57. static void Update_Temporal_Reference_Tacking_Data _ANSI_ARGS_((void));
  58. /* private variables */
  59. static int Temporal_Reference_Base = 0;
  60. static int True_Framenum_max  = -1;
  61. static int Temporal_Reference_GOP_Reset = 0;
  62.  
  63. #define RESERVED    -1 
  64. static double frame_rate_Table[16] =
  65. {
  66.   0.0,
  67.   ((23.0*1000.0)/1001.0),
  68.   24.0,
  69.   25.0,
  70.   ((30.0*1000.0)/1001.0),
  71.   30.0,
  72.   50.0,
  73.   ((60.0*1000.0)/1001.0),
  74.   60.0,
  75.  
  76.   RESERVED,
  77.   RESERVED,
  78.   RESERVED,
  79.   RESERVED,
  80.   RESERVED,
  81.   RESERVED,
  82.   RESERVED
  83. };
  84.  
  85. /*
  86.  * decode headers from one input stream
  87.  * until an End of Sequence or picture start code
  88.  * is found
  89.  */
  90. int Get_Hdr()
  91. {
  92.   unsigned int code;
  93.  
  94.   for (;;)
  95.   {
  96.     /* look for next_start_code */
  97.     next_start_code();
  98.     code = Get_Bits32();
  99.   
  100.     switch (code)
  101.     {
  102.     case SEQUENCE_HEADER_CODE:
  103.       sequence_header();
  104.       break;
  105.     case GROUP_START_CODE:
  106.       group_of_pictures_header();
  107.       break;
  108.     case PICTURE_START_CODE:
  109.       picture_header();
  110.       return 1;
  111.     case SEQUENCE_END_CODE:
  112.       return 0;
  113.     default:
  114.       if (!Quiet_Flag)
  115.         fprintf(stderr,"Unexpected next_start_code %08x (ignored)\n",code);
  116.       break;
  117.     }
  118.   }
  119. }
  120.  
  121.  
  122. /* FastForward / Rewind */
  123. int Get_Hdr_Seek(int delta)
  124. {
  125.   unsigned int code;
  126.   char grp_flag = 0;
  127.  
  128.   fseek(base.Infile, delta * 0x100000, SEEK_CUR);
  129.   Initialize_Buffer();
  130.  
  131.   for (;;) {
  132.     /* look for next_start_code */
  133.     next_start_code();
  134.     code = Get_Bits32();
  135.  
  136.     switch (code) {
  137.       case SEQUENCE_HEADER_CODE:
  138.         sequence_header();
  139.         break;
  140.       case GROUP_START_CODE:
  141.         group_of_pictures_header();
  142.         grp_flag = 1;
  143.         break;
  144.       case PICTURE_START_CODE:
  145.         picture_header();
  146.         if (grp_flag)
  147.           return 1;
  148.         break;
  149.       case SEQUENCE_END_CODE:
  150.         return 0;
  151.     }
  152.   }
  153. }
  154.  
  155.  
  156. /* align to start of next next_start_code */
  157.  
  158. void next_start_code()
  159. {
  160.   /* byte align */
  161.   Flush_Buffer(ld->Incnt&7);
  162.   while (Show_Bits(24)!=0x01L)
  163.     Flush_Buffer(8);
  164. }
  165.  
  166.  
  167. /* decode sequence header */
  168.  
  169. static void sequence_header()
  170. {
  171.   int i;
  172.   int pos;
  173.  
  174.   pos = ld->Bitcnt;
  175.   horizontal_size             = Get_Bits(12);
  176.   vertical_size               = Get_Bits(12);
  177.   aspect_ratio_information    = Get_Bits(4);
  178.   frame_rate_code             = Get_Bits(4);
  179.   bit_rate_value              = Get_Bits(18);
  180.   marker_bit("sequence_header()");
  181.   vbv_buffer_size             = Get_Bits(10);
  182.   constrained_parameters_flag = Get_Bits(1);
  183.  
  184.   if (User_Defined_FPS > 0.0)
  185.     frame_rate = User_Defined_FPS;
  186.   else
  187.     frame_rate = frame_rate_Table[frame_rate_code];
  188.  
  189.   if((ld->load_intra_quantizer_matrix = Get_Bits(1)))
  190.   {
  191.     for (i=0; i<64; i++)
  192.       ld->intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  193.   }
  194.   else
  195.   {
  196.     for (i=0; i<64; i++)
  197.       ld->intra_quantizer_matrix[i] = default_intra_quantizer_matrix[i];
  198.   }
  199.  
  200.   if((ld->load_non_intra_quantizer_matrix = Get_Bits(1)))
  201.   {
  202.     for (i=0; i<64; i++)
  203.       ld->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  204.   }
  205.   else
  206.   {
  207.     for (i=0; i<64; i++)
  208.       ld->non_intra_quantizer_matrix[i] = 16;
  209.   }
  210.  
  211.   /* copy luminance to chrominance matrices */
  212.   for (i=0; i<64; i++)
  213.   {
  214.     ld->chroma_intra_quantizer_matrix[i] =
  215.       ld->intra_quantizer_matrix[i];
  216.  
  217.     ld->chroma_non_intra_quantizer_matrix[i] =
  218.       ld->non_intra_quantizer_matrix[i];
  219.   }
  220.  
  221. #ifdef VERBOSE
  222.   if (Verbose_Flag > NO_LAYER)
  223.   {
  224.     printf("sequence header (byte %d)\n",(pos>>3)-4);
  225.     if (Verbose_Flag > SEQUENCE_LAYER)
  226.     {
  227.       printf("  horizontal_size=%d\n",horizontal_size);
  228.       printf("  vertical_size=%d\n",vertical_size);
  229.       printf("  aspect_ratio_information=%d\n",aspect_ratio_information);
  230.       printf("  frame_rate_code=%d",frame_rate_code);
  231.       printf("  bit_rate_value=%d\n",bit_rate_value);
  232.       printf("  vbv_buffer_size=%d\n",vbv_buffer_size);
  233.       printf("  constrained_parameters_flag=%d\n",constrained_parameters_flag);
  234.       printf("  load_intra_quantizer_matrix=%d\n",ld->load_intra_quantizer_matrix);
  235.       printf("  load_non_intra_quantizer_matrix=%d\n",ld->load_non_intra_quantizer_matrix);
  236.     }
  237.   }
  238. #endif /* VERBOSE */
  239.  
  240. #ifdef VERIFY
  241.   verify_sequence_header++;
  242. #endif /* VERIFY */
  243.  
  244.   extension_and_user_data();
  245. }
  246.  
  247.  
  248.  
  249. /* decode group of pictures header */
  250. /* ISO/IEC 13818-2 section 6.2.2.6 */
  251. static void group_of_pictures_header()
  252. {
  253.   int pos;
  254.  
  255.   if (ld == &base)
  256.   {
  257.     Temporal_Reference_Base = True_Framenum_max + 1;  /* *CH* */
  258.     Temporal_Reference_GOP_Reset = 1;
  259.   }
  260.   pos = ld->Bitcnt;
  261.   drop_flag   = Get_Bits(1);
  262.   hour        = Get_Bits(5);
  263.   minute      = Get_Bits(6);
  264.   marker_bit("group_of_pictures_header()");
  265.   sec         = Get_Bits(6);
  266.   frame       = Get_Bits(6);
  267.   closed_gop  = Get_Bits(1);
  268.   broken_link = Get_Bits(1);
  269.  
  270. #ifdef VERBOSE
  271.   if (Verbose_Flag > NO_LAYER)
  272.   {
  273.     printf("group of pictures (byte %d)\n",(pos>>3)-4);
  274.     if (Verbose_Flag > SEQUENCE_LAYER)
  275.     {
  276.       printf("  drop_flag=%d\n",drop_flag);
  277.       printf("  timecode %d:%02d:%02d:%02d\n",hour,minute,sec,frame);
  278.       printf("  closed_gop=%d\n",closed_gop);
  279.       printf("  broken_link=%d\n",broken_link);
  280.     }
  281.   }
  282. #endif /* VERBOSE */
  283.  
  284. #ifdef VERIFY
  285.   verify_group_of_pictures_header++;
  286. #endif /* VERIFY */
  287.  
  288.   extension_and_user_data();
  289.  
  290. }
  291.  
  292.  
  293. /* decode picture header */
  294.  
  295. /* ISO/IEC 13818-2 section 6.2.3 */
  296. static void picture_header()
  297. {
  298.   int pos;
  299.   int Extra_Information_Byte_Count;
  300.  
  301.   /* unless later overwritten by picture_spatial_scalable_extension() */
  302.   ld->pict_scal = 0; 
  303.   
  304.   pos = ld->Bitcnt;
  305.   temporal_reference  = Get_Bits(10);
  306.   picture_coding_type = Get_Bits(3);
  307.   vbv_delay           = Get_Bits(16);
  308.  
  309.   if (picture_coding_type==P_TYPE || picture_coding_type==B_TYPE)
  310.   {
  311.     full_pel_forward_vector = Get_Bits(1);
  312.     forward_f_code = Get_Bits(3);
  313.   }
  314.   if (picture_coding_type==B_TYPE)
  315.   {
  316.     full_pel_backward_vector = Get_Bits(1);
  317.     backward_f_code = Get_Bits(3);
  318.   }
  319.  
  320. #ifdef VERBOSE
  321.   if (Verbose_Flag>NO_LAYER)
  322.   {
  323.     printf("picture header (byte %d)\n",(pos>>3)-4);
  324.     if (Verbose_Flag>SEQUENCE_LAYER)
  325.     {
  326.       printf("  temporal_reference=%d\n",temporal_reference);
  327.       printf("  picture_coding_type=%d\n",picture_coding_type);
  328.       printf("  vbv_delay=%d\n",vbv_delay);
  329.       if (picture_coding_type==P_TYPE || picture_coding_type==B_TYPE)
  330.       {
  331.         printf("  full_pel_forward_vector=%d\n",full_pel_forward_vector);
  332.         printf("  forward_f_code =%d\n",forward_f_code);
  333.       }
  334.       if (picture_coding_type==B_TYPE)
  335.       {
  336.         printf("  full_pel_backward_vector=%d\n",full_pel_backward_vector);
  337.         printf("  backward_f_code =%d\n",backward_f_code);
  338.       }
  339.     }
  340.   }
  341. #endif /* VERBOSE */
  342.  
  343. #ifdef VERIFY
  344.   verify_picture_header++;
  345. #endif /* VERIFY */
  346.  
  347.   Extra_Information_Byte_Count = 
  348.     extra_bit_information();
  349.   
  350.   extension_and_user_data();
  351.  
  352.   /* update tracking information used to assist spatial scalability */
  353.   Update_Temporal_Reference_Tacking_Data();
  354. }
  355.  
  356. /* decode slice header */
  357.  
  358. /* ISO/IEC 13818-2 section 6.2.4 */
  359. int slice_header()
  360. {
  361.   int slice_vertical_position_extension;
  362.   int quantizer_scale_code;
  363.   int pos;
  364.   int slice_picture_id_enable = 0;
  365.   int slice_picture_id = 0;
  366.   int extra_information_slice = 0;
  367.  
  368.   pos = ld->Bitcnt;
  369.  
  370.   slice_vertical_position_extension =
  371.     (ld->MPEG2_Flag && vertical_size>2800) ? Get_Bits(3) : 0;
  372.  
  373.   if (ld->scalable_mode==SC_DP)
  374.     ld->priority_breakpoint = Get_Bits(7);
  375.  
  376.   quantizer_scale_code = Get_Bits(5);
  377.   ld->quantizer_scale =
  378.     ld->MPEG2_Flag ? (ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1) : quantizer_scale_code;
  379.  
  380.   /* slice_id introduced in March 1995 as part of the video corridendum
  381.      (after the IS was drafted in November 1994) */
  382.   if (Get_Bits(1))
  383.   {
  384.     ld->intra_slice = Get_Bits(1);
  385.  
  386.     slice_picture_id_enable = Get_Bits(1);
  387.   slice_picture_id = Get_Bits(6);
  388.  
  389.     extra_information_slice = extra_bit_information();
  390.   }
  391.   else
  392.     ld->intra_slice = 0;
  393.  
  394. #ifdef VERBOSE
  395.   if (Verbose_Flag>PICTURE_LAYER)
  396.   {
  397.     printf("slice header (byte %d)\n",(pos>>3)-4);
  398.     if (Verbose_Flag>SLICE_LAYER)
  399.     {
  400.       if (ld->MPEG2_Flag && vertical_size>2800)
  401.         printf("  slice_vertical_position_extension=%d\n",slice_vertical_position_extension);
  402.   
  403.       if (ld->scalable_mode==SC_DP)
  404.         printf("  priority_breakpoint=%d\n",ld->priority_breakpoint);
  405.  
  406.       printf("  quantizer_scale_code=%d\n",quantizer_scale_code);
  407.  
  408.       printf("  slice_picture_id_enable = %d\n", slice_picture_id_enable);
  409.  
  410.       if(slice_picture_id_enable)
  411.         printf("  slice_picture_id = %d\n", slice_picture_id);
  412.  
  413.     }
  414.   }
  415. #endif /* VERBOSE */
  416.  
  417. #ifdef VERIFY
  418.   verify_slice_header++;
  419. #endif /* VERIFY */
  420.  
  421.  
  422.   return slice_vertical_position_extension;
  423. }
  424.  
  425.  
  426. /* decode extension and user data */
  427. /* ISO/IEC 13818-2 section 6.2.2.2 */
  428. static void extension_and_user_data()
  429. {
  430.   int code,ext_ID;
  431.  
  432.   next_start_code();
  433.  
  434.   while ((code = Show_Bits(32))==EXTENSION_START_CODE || code==USER_DATA_START_CODE)
  435.   {
  436.     if (code==EXTENSION_START_CODE)
  437.     {
  438.       Flush_Buffer32();
  439.       ext_ID = Get_Bits(4);
  440.       switch (ext_ID)
  441.       {
  442.       case SEQUENCE_EXTENSION_ID:
  443.         sequence_extension();
  444.         break;
  445.       case SEQUENCE_DISPLAY_EXTENSION_ID:
  446.         sequence_display_extension();
  447.         break;
  448.       case QUANT_MATRIX_EXTENSION_ID:
  449.         quant_matrix_extension();
  450.         break;
  451.       case SEQUENCE_SCALABLE_EXTENSION_ID:
  452.         sequence_scalable_extension();
  453.         break;
  454.       case PICTURE_DISPLAY_EXTENSION_ID:
  455.         picture_display_extension();
  456.         break;
  457.       case PICTURE_CODING_EXTENSION_ID:
  458.         picture_coding_extension();
  459.         break;
  460.       case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
  461.         picture_spatial_scalable_extension();
  462.         break;
  463.       case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
  464.         picture_temporal_scalable_extension();
  465.         break;
  466.       case COPYRIGHT_EXTENSION_ID:
  467.         copyright_extension();
  468.         break;
  469.      default:
  470.         fprintf(stderr,"reserved extension start code ID %d\n",ext_ID);
  471.         break;
  472.       }
  473.       next_start_code();
  474.     }
  475.     else
  476.     {
  477. #ifdef VERBOSE
  478.       if (Verbose_Flag>NO_LAYER)
  479.         printf("user data\n");
  480. #endif /* VERBOSE */
  481.       Flush_Buffer32();
  482.       user_data();
  483.     }
  484.   }
  485. }
  486.  
  487.  
  488. /* decode sequence extension */
  489.  
  490. /* ISO/IEC 13818-2 section 6.2.2.3 */
  491. static void sequence_extension()
  492. {
  493.   int horizontal_size_extension;
  494.   int vertical_size_extension;
  495.   int bit_rate_extension;
  496.   int vbv_buffer_size_extension;
  497.   int pos;
  498.  
  499.   /* derive bit position for trace */
  500. #ifdef VERBOSE
  501.   pos = ld->Bitcnt;
  502. #endif
  503.  
  504.   ld->MPEG2_Flag = 1;
  505.  
  506.   ld->scalable_mode = SC_NONE; /* unless overwritten by sequence_scalable_extension() */
  507.   layer_id = 0;                /* unless overwritten by sequence_scalable_extension() */
  508.   
  509.   profile_and_level_indication = Get_Bits(8);
  510.   progressive_sequence         = Get_Bits(1);
  511.   chroma_format                = Get_Bits(2);
  512.   horizontal_size_extension    = Get_Bits(2);
  513.   vertical_size_extension      = Get_Bits(2);
  514.   bit_rate_extension           = Get_Bits(12);
  515.   marker_bit("sequence_extension");
  516.   vbv_buffer_size_extension    = Get_Bits(8);
  517.   low_delay                    = Get_Bits(1);
  518.   frame_rate_extension_n       = Get_Bits(2);
  519.   frame_rate_extension_d       = Get_Bits(5);
  520.  
  521.   if (User_Defined_FPS > 0.0)
  522.     frame_rate = User_Defined_FPS;
  523.   else
  524.     frame_rate = frame_rate_Table[frame_rate_code] *
  525.                  ((frame_rate_extension_n+1)/(frame_rate_extension_d+1));
  526.  
  527.   /* special case for 422 profile & level must be made */
  528.   if((profile_and_level_indication>>7) & 1)
  529.   {  /* escape bit of profile_and_level_indication set */
  530.   
  531.     /* 4:2:2 Profile @ Main Level */
  532.     if((profile_and_level_indication&15)==5)
  533.     {
  534.       profile = PROFILE_422;
  535.       level   = MAIN_LEVEL;  
  536.     }
  537.   }
  538.   else
  539.   {
  540.     profile = profile_and_level_indication >> 4;  /* Profile is upper nibble */
  541.     level   = profile_and_level_indication & 0xF;  /* Level is lower nibble */
  542.   }
  543.   
  544.  
  545.   horizontal_size = (horizontal_size_extension<<12) | (horizontal_size&0x0fff);
  546.   vertical_size = (vertical_size_extension<<12) | (vertical_size&0x0fff);
  547.  
  548.  
  549.   /* ISO/IEC 13818-2 does not define bit_rate_value to be composed of
  550.    * both the original bit_rate_value parsed in sequence_header() and
  551.    * the optional bit_rate_extension in sequence_extension_header(). 
  552.    * However, we use it for bitstream verification purposes. 
  553.    */
  554.  
  555.   bit_rate_value += (bit_rate_extension << 18);
  556.   bit_rate = ((double) bit_rate_value) * 400.0;
  557.   vbv_buffer_size += (vbv_buffer_size_extension << 10);
  558.  
  559. #ifdef VERBOSE
  560.   if (Verbose_Flag>NO_LAYER)
  561.   {
  562.     printf("sequence extension (byte %d)\n",(pos>>3)-4);
  563.  
  564.     if (Verbose_Flag>SEQUENCE_LAYER)
  565.     {
  566.       printf("  profile_and_level_indication=%d\n",profile_and_level_indication);
  567.  
  568.       if (profile_and_level_indication<128)
  569.       {
  570.         printf("    profile=%d, level=%d\n",profile,level);
  571.       }
  572.  
  573.       printf("  progressive_sequence=%d\n",progressive_sequence);
  574.       printf("  chroma_format=%d\n",chroma_format);
  575.       printf("  horizontal_size_extension=%d\n",horizontal_size_extension);
  576.       printf("  vertical_size_extension=%d\n",vertical_size_extension);
  577.       printf("  bit_rate_extension=%d\n",bit_rate_extension);
  578.       printf("  vbv_buffer_size_extension=%d\n",vbv_buffer_size_extension);
  579.       printf("  low_delay=%d\n",low_delay);
  580.       printf("  frame_rate_extension_n=%d\n",frame_rate_extension_n);
  581.       printf("  frame_rate_extension_d=%d\n",frame_rate_extension_d);
  582.     }
  583.   }
  584. #endif /* VERBOSE */
  585.  
  586. #ifdef VERIFY
  587.   verify_sequence_extension++;
  588. #endif /* VERIFY */
  589.  
  590.  
  591. }
  592.  
  593.  
  594. /* decode sequence display extension */
  595.  
  596. static void sequence_display_extension()
  597. {
  598.   int pos;
  599.  
  600.   pos = ld->Bitcnt;
  601.   video_format      = Get_Bits(3);
  602.   color_description = Get_Bits(1);
  603.  
  604.   if (color_description)
  605.   {
  606.     color_primaries          = Get_Bits(8);
  607.     transfer_characteristics = Get_Bits(8);
  608.     matrix_coefficients      = Get_Bits(8);
  609.   }
  610.  
  611.   display_horizontal_size = Get_Bits(14);
  612.   marker_bit("sequence_display_extension");
  613.   display_vertical_size   = Get_Bits(14);
  614.  
  615. #ifdef VERBOSE
  616.   if (Verbose_Flag>NO_LAYER)
  617.   {
  618.     printf("sequence display extension (byte %d)\n",(pos>>3)-4);
  619.     if (Verbose_Flag>SEQUENCE_LAYER)
  620.     {
  621.  
  622.       printf("  video_format=%d\n",video_format);
  623.       printf("  color_description=%d\n",color_description);
  624.  
  625.       if (color_description)
  626.       {
  627.         printf("    color_primaries=%d\n",color_primaries);
  628.         printf("    transfer_characteristics=%d\n",transfer_characteristics);
  629.         printf("    matrix_coefficients=%d\n",matrix_coefficients);
  630.       }
  631.       printf("  display_horizontal_size=%d\n",display_horizontal_size);
  632.       printf("  display_vertical_size=%d\n",display_vertical_size);
  633.     }
  634.   }
  635. #endif /* VERBOSE */
  636.  
  637. #ifdef VERIFY
  638.   verify_sequence_display_extension++;
  639. #endif /* VERIFY */
  640.  
  641. }
  642.  
  643.  
  644. /* decode quant matrix entension */
  645. /* ISO/IEC 13818-2 section 6.2.3.2 */
  646. static void quant_matrix_extension()
  647. {
  648.   int i;
  649.   int pos;
  650.  
  651.   pos = ld->Bitcnt;
  652.  
  653.   if((ld->load_intra_quantizer_matrix = Get_Bits(1)))
  654.   {
  655.     for (i=0; i<64; i++)
  656.     {
  657.       ld->chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  658.       = ld->intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  659.       = Get_Bits(8);
  660.     }
  661.   }
  662.  
  663.   if((ld->load_non_intra_quantizer_matrix = Get_Bits(1)))
  664.   {
  665.     for (i=0; i<64; i++)
  666.     {
  667.       ld->chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  668.       = ld->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  669.       = Get_Bits(8);
  670.     }
  671.   }
  672.  
  673.   if((ld->load_chroma_intra_quantizer_matrix = Get_Bits(1)))
  674.   {
  675.     for (i=0; i<64; i++)
  676.       ld->chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  677.   }
  678.  
  679.   if((ld->load_chroma_non_intra_quantizer_matrix = Get_Bits(1)))
  680.   {
  681.     for (i=0; i<64; i++)
  682.       ld->chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  683.   }
  684.  
  685. #ifdef VERBOSE
  686.   if (Verbose_Flag>NO_LAYER)
  687.   {
  688.     printf("quant matrix extension (byte %d)\n",(pos>>3)-4);
  689.     printf("  load_intra_quantizer_matrix=%d\n",
  690.       ld->load_intra_quantizer_matrix);
  691.     printf("  load_non_intra_quantizer_matrix=%d\n",
  692.       ld->load_non_intra_quantizer_matrix);
  693.     printf("  load_chroma_intra_quantizer_matrix=%d\n",
  694.       ld->load_chroma_intra_quantizer_matrix);
  695.     printf("  load_chroma_non_intra_quantizer_matrix=%d\n",
  696.       ld->load_chroma_non_intra_quantizer_matrix);
  697.   }
  698. #endif /* VERBOSE */
  699.  
  700. #ifdef VERIFY
  701.   verify_quant_matrix_extension++;
  702. #endif /* VERIFY */
  703.  
  704. }
  705.  
  706.  
  707. /* decode sequence scalable extension */
  708. /* ISO/IEC 13818-2   section 6.2.2.5 */
  709. static void sequence_scalable_extension()
  710. {
  711.   int pos;
  712.  
  713.   pos = ld->Bitcnt;
  714.  
  715.   /* values (without the +1 offset) of scalable_mode are defined in 
  716.      Table 6-10 of ISO/IEC 13818-2 */
  717.   ld->scalable_mode = Get_Bits(2) + 1; /* add 1 to make SC_DP != SC_NONE */
  718.  
  719.   layer_id = Get_Bits(4);
  720.  
  721.   if (ld->scalable_mode==SC_SPAT)
  722.   {
  723.     lower_layer_prediction_horizontal_size = Get_Bits(14);
  724.     marker_bit("sequence_scalable_extension()");
  725.     lower_layer_prediction_vertical_size   = Get_Bits(14); 
  726.     horizontal_subsampling_factor_m        = Get_Bits(5);
  727.     horizontal_subsampling_factor_n        = Get_Bits(5);
  728.     vertical_subsampling_factor_m          = Get_Bits(5);
  729.     vertical_subsampling_factor_n          = Get_Bits(5);
  730.   }
  731.  
  732.   if (ld->scalable_mode==SC_TEMP)
  733.     Error("temporal scalability not implemented\n");
  734.  
  735. #ifdef VERBOSE
  736.   if (Verbose_Flag>NO_LAYER)
  737.   {
  738.     printf("sequence scalable extension (byte %d)\n",(pos>>3)-4);
  739.     if (Verbose_Flag>SEQUENCE_LAYER)
  740.     {
  741.       printf("  scalable_mode=%d\n",ld->scalable_mode-1);
  742.       printf("  layer_id=%d\n",layer_id);
  743.       if (ld->scalable_mode==SC_SPAT)
  744.       {
  745.         printf("    lower_layer_prediction_horiontal_size=%d\n",
  746.           lower_layer_prediction_horizontal_size);
  747.         printf("    lower_layer_prediction_vertical_size=%d\n",
  748.           lower_layer_prediction_vertical_size);
  749.         printf("    horizontal_subsampling_factor_m=%d\n",
  750.           horizontal_subsampling_factor_m);
  751.         printf("    horizontal_subsampling_factor_n=%d\n",
  752.           horizontal_subsampling_factor_n);
  753.         printf("    vertical_subsampling_factor_m=%d\n",
  754.           vertical_subsampling_factor_m);
  755.         printf("    vertical_subsampling_factor_n=%d\n",
  756.           vertical_subsampling_factor_n);
  757.       }
  758.     }
  759.   }
  760. #endif /* VERBOSE */
  761.  
  762. #ifdef VERIFY
  763.   verify_sequence_scalable_extension++;
  764. #endif /* VERIFY */
  765.  
  766. }
  767.  
  768.  
  769. /* decode picture display extension */
  770. /* ISO/IEC 13818-2 section 6.2.3.3. */
  771. static void picture_display_extension()
  772. {
  773.   int i;
  774.   int number_of_frame_center_offsets;
  775.   int pos;
  776.  
  777.   pos = ld->Bitcnt;
  778.   /* based on ISO/IEC 13818-2 section 6.3.12 
  779.     (November 1994) Picture display extensions */
  780.  
  781.   /* derive number_of_frame_center_offsets */
  782.   if(progressive_sequence)
  783.   {
  784.     if(repeat_first_field)
  785.     {
  786.       if(top_field_first)
  787.         number_of_frame_center_offsets = 3;
  788.       else
  789.         number_of_frame_center_offsets = 2;
  790.     }
  791.     else
  792.     {
  793.       number_of_frame_center_offsets = 1;
  794.     }
  795.   }
  796.   else
  797.   {
  798.     if(picture_structure!=FRAME_PICTURE)
  799.     {
  800.       number_of_frame_center_offsets = 1;
  801.     }
  802.     else
  803.     {
  804.       if(repeat_first_field)
  805.         number_of_frame_center_offsets = 3;
  806.       else
  807.         number_of_frame_center_offsets = 2;
  808.     }
  809.   }
  810.  
  811.  
  812.   /* now parse */
  813.   for (i=0; i<number_of_frame_center_offsets; i++)
  814.   {
  815.     frame_center_horizontal_offset[i] = Get_Bits(16);
  816.     marker_bit("picture_display_extension, first marker bit");
  817.     
  818.     frame_center_vertical_offset[i]   = Get_Bits(16);
  819.     marker_bit("picture_display_extension, second marker bit");
  820.   }
  821.  
  822. #ifdef VERBOSE
  823.   if (Verbose_Flag>NO_LAYER)
  824.   {
  825.     printf("picture display extension (byte %d)\n",(pos>>3)-4);
  826.     if (Verbose_Flag>SEQUENCE_LAYER)
  827.     {
  828.  
  829.       for (i=0; i<number_of_frame_center_offsets; i++)
  830.       {
  831.         printf("  frame_center_horizontal_offset[%d]=%d\n",i,
  832.           frame_center_horizontal_offset[i]);
  833.         printf("  frame_center_vertical_offset[%d]=%d\n",i,
  834.           frame_center_vertical_offset[i]);
  835.       }
  836.     }
  837.   }
  838. #endif /* VERBOSE */
  839.  
  840. #ifdef VERIFY
  841.   verify_picture_display_extension++;
  842. #endif /* VERIFY */
  843.  
  844. }
  845.  
  846.  
  847. /* decode picture coding extension */
  848. static void picture_coding_extension()
  849. {
  850.   int pos;
  851.  
  852.   pos = ld->Bitcnt;
  853.  
  854.   f_code[0][0] = Get_Bits(4);
  855.   f_code[0][1] = Get_Bits(4);
  856.   f_code[1][0] = Get_Bits(4);
  857.   f_code[1][1] = Get_Bits(4);
  858.  
  859.   intra_dc_precision         = Get_Bits(2);
  860.   picture_structure          = Get_Bits(2);
  861.   top_field_first            = Get_Bits(1);
  862.   frame_pred_frame_dct       = Get_Bits(1);
  863.   concealment_motion_vectors = Get_Bits(1);
  864.   ld->q_scale_type           = Get_Bits(1);
  865.   intra_vlc_format           = Get_Bits(1);
  866.   ld->alternate_scan         = Get_Bits(1);
  867.   repeat_first_field         = Get_Bits(1);
  868.   chroma_420_type            = Get_Bits(1);
  869.   progressive_frame          = Get_Bits(1);
  870.   composite_display_flag     = Get_Bits(1);
  871.  
  872.   if (composite_display_flag)
  873.   {
  874.     v_axis            = Get_Bits(1);
  875.     field_sequence    = Get_Bits(3);
  876.     sub_carrier       = Get_Bits(1);
  877.     burst_amplitude   = Get_Bits(7);
  878.     sub_carrier_phase = Get_Bits(8);
  879.   }
  880.  
  881. #ifdef VERBOSE
  882.   if (Verbose_Flag>NO_LAYER)
  883.   {
  884.     printf("picture coding extension (byte %d)\n",(pos>>3)-4);
  885.     if (Verbose_Flag>SEQUENCE_LAYER)
  886.     {
  887.       printf("  forward horizontal f_code=%d\n", f_code[0][0]);
  888.       printf("  forward vertical f_code=%d\n", f_code[0][1]);
  889.       printf("  backward horizontal f_code=%d\n", f_code[1][0]);
  890.       printf("  backward_vertical f_code=%d\n", f_code[1][1]);
  891.       printf("  intra_dc_precision=%d\n",intra_dc_precision);
  892.       printf("  picture_structure=%d\n",picture_structure);
  893.       printf("  top_field_first=%d\n",top_field_first);
  894.       printf("  frame_pred_frame_dct=%d\n",frame_pred_frame_dct);
  895.       printf("  concealment_motion_vectors=%d\n",concealment_motion_vectors);
  896.       printf("  q_scale_type=%d\n",ld->q_scale_type);
  897.       printf("  intra_vlc_format=%d\n",intra_vlc_format);
  898.       printf("  alternate_scan=%d\n",ld->alternate_scan);
  899.       printf("  repeat_first_field=%d\n",repeat_first_field);
  900.       printf("  chroma_420_type=%d\n",chroma_420_type);
  901.       printf("  progressive_frame=%d\n",progressive_frame);
  902.       printf("  composite_display_flag=%d\n",composite_display_flag);
  903.  
  904.       if (composite_display_flag)
  905.       {
  906.         printf("    v_axis=%d\n",v_axis);
  907.         printf("    field_sequence=%d\n",field_sequence);
  908.         printf("    sub_carrier=%d\n",sub_carrier);
  909.         printf("    burst_amplitude=%d\n",burst_amplitude);
  910.         printf("    sub_carrier_phase=%d\n",sub_carrier_phase);
  911.       }
  912.     }
  913.   }
  914. #endif /* VERBOSE */
  915.  
  916. #ifdef VERIFY
  917.   verify_picture_coding_extension++;
  918. #endif /* VERIFY */
  919. }
  920.  
  921.  
  922. /* decode picture spatial scalable extension */
  923. /* ISO/IEC 13818-2 section 6.2.3.5. */
  924. static void picture_spatial_scalable_extension()
  925. {
  926.   int pos;
  927.  
  928.   pos = ld->Bitcnt;
  929.  
  930.   ld->pict_scal = 1; /* use spatial scalability in this picture */
  931.  
  932.   lower_layer_temporal_reference = Get_Bits(10);
  933.   marker_bit("picture_spatial_scalable_extension(), first marker bit");
  934.   lower_layer_horizontal_offset = Get_Bits(15);
  935.   if (lower_layer_horizontal_offset>=16384)
  936.     lower_layer_horizontal_offset-= 32768;
  937.   marker_bit("picture_spatial_scalable_extension(), second marker bit");
  938.   lower_layer_vertical_offset = Get_Bits(15);
  939.   if (lower_layer_vertical_offset>=16384)
  940.     lower_layer_vertical_offset-= 32768;
  941.   spatial_temporal_weight_code_table_index = Get_Bits(2);
  942.   lower_layer_progressive_frame = Get_Bits(1);
  943.   lower_layer_deinterlaced_field_select = Get_Bits(1);
  944.  
  945. #ifdef VERBOSE
  946.   if (Verbose_Flag>NO_LAYER)
  947.   {
  948.     printf("picture spatial scalable extension (byte %d)\n",(pos>>3)-4);
  949.     if (Verbose_Flag>SEQUENCE_LAYER)
  950.     {
  951.       printf("  lower_layer_temporal_reference=%d\n",lower_layer_temporal_reference);
  952.       printf("  lower_layer_horizontal_offset=%d\n",lower_layer_horizontal_offset);
  953.       printf("  lower_layer_vertical_offset=%d\n",lower_layer_vertical_offset);
  954.       printf("  spatial_temporal_weight_code_table_index=%d\n",
  955.         spatial_temporal_weight_code_table_index);
  956.       printf("  lower_layer_progressive_frame=%d\n",lower_layer_progressive_frame);
  957.       printf("  lower_layer_deinterlaced_field_select=%d\n",lower_layer_deinterlaced_field_select);
  958.     }
  959.   }
  960. #endif /* VERBOSE */
  961.  
  962. #ifdef VERIFY
  963.   verify_picture_spatial_scalable_extension++;
  964. #endif /* VERIFY */
  965.  
  966. }
  967.  
  968.  
  969. /* decode picture temporal scalable extension
  970.  *
  971.  * not implemented
  972.  */
  973. /* ISO/IEC 13818-2 section 6.2.3.4. */
  974. static void picture_temporal_scalable_extension()
  975. {
  976.   Error("temporal scalability not supported\n");
  977.  
  978. #ifdef VERIFY
  979.   verify_picture_temporal_scalable_extension++;
  980. #endif /* VERIFY */
  981. }
  982.  
  983.  
  984. /* decode extra bit information */
  985. /* ISO/IEC 13818-2 section 6.2.3.4. */
  986. static int extra_bit_information()
  987. {
  988.   int Byte_Count = 0;
  989.  
  990.   while (Get_Bits1())
  991.   {
  992.     Flush_Buffer(8);
  993.     Byte_Count++;
  994.   }
  995.  
  996.   return(Byte_Count);
  997. }
  998.  
  999.  
  1000.  
  1001. /* ISO/IEC 13818-2 section 5.3 */
  1002. /* Purpose: this function is mainly designed to aid in bitstream conformance
  1003.    testing.  A simple Flush_Buffer(1) would do */
  1004. void marker_bit(text)
  1005. char *text;
  1006. {
  1007.   int marker;
  1008.  
  1009.   marker = Get_Bits(1);
  1010.  
  1011. #ifdef VERIFY  
  1012.   if(!marker)
  1013.     printf("ERROR: %s--marker_bit set to 0",text);
  1014. #endif
  1015. }
  1016.  
  1017.  
  1018. /* ISO/IEC 13818-2  sections 6.3.4.1 and 6.2.2.2.2 */
  1019. static void user_data()
  1020. {
  1021.   /* skip ahead to the next start code */
  1022.   next_start_code();
  1023. }
  1024.  
  1025.  
  1026.  
  1027. /* Copyright extension */
  1028. /* ISO/IEC 13818-2 section 6.2.3.6. */
  1029. /* (header added in November, 1994 to the IS document) */
  1030.  
  1031.  
  1032. static void copyright_extension()
  1033. {
  1034.   int pos;
  1035.   int reserved_data;
  1036.  
  1037.   pos = ld->Bitcnt;
  1038.   
  1039.  
  1040.   copyright_flag =       Get_Bits(1); 
  1041.   copyright_identifier = Get_Bits(8);
  1042.   original_or_copy =     Get_Bits(1);
  1043.   
  1044.   /* reserved */
  1045.   reserved_data = Get_Bits(7);
  1046.  
  1047.   marker_bit("copyright_extension(), first marker bit");
  1048.   copyright_number_1 =   Get_Bits(20);
  1049.   marker_bit("copyright_extension(), second marker bit");
  1050.   copyright_number_2 =   Get_Bits(22);
  1051.   marker_bit("copyright_extension(), third marker bit");
  1052.   copyright_number_3 =   Get_Bits(22);
  1053.  
  1054.   if(Verbose_Flag>NO_LAYER)
  1055.   {
  1056.     printf("copyright_extension (byte %d)\n",(pos>>3)-4);
  1057.     if (Verbose_Flag>SEQUENCE_LAYER)
  1058.     {
  1059.       printf("  copyright_flag =%d\n",copyright_flag);
  1060.         
  1061.       printf("  copyright_identifier=%d\n",copyright_identifier);
  1062.         
  1063.       printf("  original_or_copy = %d (original=1, copy=0)\n",
  1064.         original_or_copy);
  1065.         
  1066.       printf("  copyright_number_1=%d\n",copyright_number_1);
  1067.       printf("  copyright_number_2=%d\n",copyright_number_2);
  1068.       printf("  copyright_number_3=%d\n",copyright_number_3);
  1069.     }
  1070.   }
  1071.  
  1072. #ifdef VERIFY
  1073.   verify_copyright_extension++;
  1074. #endif /* VERIFY */
  1075. }
  1076.  
  1077.  
  1078.  
  1079. /* introduced in September 1995 to assist Spatial Scalability */
  1080. static void Update_Temporal_Reference_Tacking_Data()
  1081. {
  1082.   static int temporal_reference_wrap  = 0;
  1083.   static int temporal_reference_old   = 0;
  1084.  
  1085.   if (ld == &base)      /* *CH* */
  1086.   {
  1087.     if (picture_coding_type!=B_TYPE && temporal_reference!=temporal_reference_old)  
  1088.     /* check first field of */
  1089.     {             
  1090.        /* non-B-frame */
  1091.       if (temporal_reference_wrap)    
  1092.       {/* wrap occured at previous I- or P-frame */ 
  1093.        /* now all intervening B-frames which could 
  1094.           still have high temporal_reference values are done  */
  1095.         Temporal_Reference_Base += 1024;
  1096.       temporal_reference_wrap = 0;
  1097.       }
  1098.       
  1099.       /* distinguish from a reset */
  1100.       if (temporal_reference<temporal_reference_old && !Temporal_Reference_GOP_Reset) 
  1101.       temporal_reference_wrap = 1;  /* we must have just passed a GOP-Header! */
  1102.       
  1103.       temporal_reference_old = temporal_reference;
  1104.       Temporal_Reference_GOP_Reset = 0;
  1105.     }
  1106.  
  1107.     True_Framenum = Temporal_Reference_Base + temporal_reference;
  1108.     
  1109.     /* temporary wrap of TR at 1024 for M frames */
  1110.     if (temporal_reference_wrap && temporal_reference <= temporal_reference_old)  
  1111.       True_Framenum += 1024;        
  1112.  
  1113.     True_Framenum_max = (True_Framenum > True_Framenum_max) ?
  1114.                         True_Framenum : True_Framenum_max;
  1115.   }
  1116. }
  1117.